package com.huang.pc;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/*
* A执行完调用b在调用C
*
*
* */
public class C {
    public static void main(String[] args) {
        Data3 data3=new  Data3();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                data3.printA();
            }
        },"A").start();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                data3.printB();
            }
        },"B").start();
        new Thread(()->{
            for (int i = 0; i < 10; i++) {
                data3.printC();
            }
        },"C").start();
    }
}

class Data3{
    private int num=0;
    Lock lock=new ReentrantLock();
    Condition condition1 = lock.newCondition();
    Condition condition2 = lock.newCondition();
    Condition condition3 = lock.newCondition();

    public void printA(){
        lock.lock();
        try{
            while (num!=0){condition1.await();}
            num=1;
            System.out.println(Thread.currentThread().getName()+"======="+"A");
            condition2.signal();
        }catch (Exception e){}finally {
            lock.unlock();
        }

    }
    public void printB(){
        lock.lock();
        try{
            while (num!=1){condition2.await();}
            num=2;
            System.out.println(Thread.currentThread().getName()+"======="+"B");
            condition3.signal();
        }catch (Exception e){}finally {
            lock.unlock();
        }


    }
    public void printC(){
        lock.lock();
        try{
            while (num!=2){condition3.await();}
            num=0;
            System.out.println(Thread.currentThread().getName()+"======="+"C");
            condition1.signal();
        }catch (Exception e){}finally {
            lock.unlock();
        }
    }

}
